/*
 * CS_MAIL_DELETE_ALL.cpp
 *
 *  Created on: 2013. 10. 10.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NState/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_MAIL_DELETE_ALL(User * pUser, ::Network::Packet * pPacket)
{

/*	//받은 편지 삭제
	//아이템 미첨부만 삭제
	CS_MAIL_DELETE_ALL			0x120C
	{

	}

	//받은 편지 삭제
	SC_MAIL_DELETE_ALL			0x120D
	{
		s16 			s16ack;	 						// 1: 성공,  -1: 실패
	}*/

	PREPARE_PACKET;

	pUser->SetState(NState::Static::Instance().MAILSTATE());

	Database::Command * pCommand = Database::GETCOMMANDPOOL()->NEW();

	pCommand->SetQueryType(::Database::Command::UPDATE);
	pCommand->MakeQuery("UPDATE data_mail SET mail_status=2 WHERE recv_user_seq = %d AND gift_status = 0 AND mail_status = 1",
			pUser->GetUserSeq());
	pCommand->SetObserver(pUser);
	pCommand->SetCommandId(DB_MAIL_DELETE_ALL);

	if (false == Database::GETSESSION()->AsyncExecute(pUser->GetUserInfo().shard_Item_db_type, pCommand))
	{
		DEBUGLOG("%s : AsyncExecute Fail", __FUNCTION__);

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_DB_ERROR);
		pAck->MakePacket(CMD_SC_MAIL_DELETE_ALL);
		pUser->Send(pAck);

		pUser->SetState(NState::Static::Instance().MAINFRIENDLIST());

		Database::GETCOMMANDPOOL()->DEL(pCommand);

		return;
	}



#if 0
	NLogic::MailBox * pMailBox = pUser->GetMailBox();
	if (NULL == pMailBox)
	{
		DEBUGLOG("CS_MAIL_DELETE_ALL : pMailBox is NULL!!");
		return;
	}

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::UPDATE);
	clsAuto->MakeQuery("UPDATE data_mail SET mail_status=2 WHERE recv_user_seq = %d AND gift_status = 0 AND mail_status = 1",
			pUser->GetUserInfo().userseq);

	if (false == ::Database::GETSESSION()->SyncExecute(pUser->GetUserInfo().shard_Item_db_type, clsAuto))
	{
		DEBUGLOG("CS_MAIL_DELETE_ALL : false == ::Database::GETSESSION()->SyncExecute(1, clsAuto)");
		return;
	}

	if (clsAuto->GetErrorIdx() != 0)
	{
		DEBUGLOG("CS_MAIL_DELETE_ALL : clsAuto->GetErrorIdx() != 0");
		return;
	}

	NLogic::MailBox::MAILTREE::const_iterator citer = pMailBox->GetMailTree().begin();

	for ( ; citer != pMailBox->GetMailTree().end(); ++citer)
	{
		// 아이템이 첨부 되어 있는 메일은 삭제 하지 않는다.
		if (0 != citer->second->GetData().gift_status)
			continue;

		// 읽지 않은 메일은 삭제하지 않는다
		if (0 == citer->second->GetData().mail_status)
			continue;

		pMailBox->DelMailData(citer->first);
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_MAIL_DELETE_ALL);
	pUser->Send(pAck);
#endif
};


} /*NCommand*/ } /*ProjectZ*/
